/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using VSLangProj80;
using EnvDTE;
using System.IO;
using DaveSexton.DocProject.Wizards.Foundation;
using DaveSexton.DocProject.Wizards;
using System.Text.RegularExpressions;
using System.Xml.XPath;
using System.Xml;
using DaveSexton.DocProject.Sandcastle.Configuration;

namespace DaveSexton.DocProject.Sandcastle
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
	public sealed class ChoosePresentationWizardStep : WizardStep
	{
		#region Public Properties
		public override string Name
		{
			get
			{
				return Resources.WizardText.ChoosePresentationStepName;
			}
		}

		public override bool Visible
		{
			get
			{
				return Control.PresentationCount > 0;
			}
		}

		public Presentation Presentation
		{
			get
			{
				return Control.SelectedPresentation;
			}
		}

		public override bool CanPreloadUserInterface
		{
			get
			{
				return true;
			}
		}
		#endregion

		#region Private / Protected
		private ChoosePresentationControl Control
		{
			get
			{
				if (control == null)
					control = new ChoosePresentationControl();

				return control;
			}
		}

		private ChoosePresentationControl control;
		private readonly IDocProject project;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ChoosePresentationWizardStep" /> class.
		/// </summary>
		public ChoosePresentationWizardStep(IDocProject project)
		{
			if (project == null)
				throw new ArgumentNullException("project");

			this.project = project;
		}
		#endregion

		#region Methods
		protected override object GetUserInterface(WizardController controller, bool preloading)
		{
			return Control;
		}

		protected override bool CanNavigateAway(WizardController controller, WizardStep toStep, bool forward)
		{
			return !forward || Control.SelectedPresentation != null;
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		protected override void WizardFinished(WizardController controller)
		{
			SandcastleProjectOptions.SetSandcastleVersion(project, BuildAssemblerLoader.Version);

			Presentation presentation = Control.SelectedPresentation;

			try
			{
				controller.Data["importResults"] = presentation.Import(project);
			}
			catch (Exception ex)
			{
				Log.Exception(ex, Resources.Errors.ImportProjectFailed);

				DocProjectEnvironment.ShowErrorDialogWithCaption(ex, Resources.Text.ImportProjectFailedCaption,
					Resources.Errors.ImportProjectFailed);
			}

			SandcastleBuildEngineProvider.DefaultPresentation = presentation;
			SandcastleBuildEngineProvider.Configuration.SaveChanges();
		}

		protected override void WizardCanceled(WizardController controller)
		{
			// do nothing
		}
		#endregion
	}
}
